import { useTranslation } from "react-i18next";
import { BasicTable, useTable, BasicColumn, FormSchema } from "@/components/Table";
import { Switch } from "antd";
import TableAction from "@/components/Table/components/TableAction";
import { get_emergencies_page, del_emergencies, update_emergencies } from "@/api/EmergencyDictionary";
import { get_patients } from "@/api/components/patientInformation";
import DetailDrawer_history from "./components/DrawerHistory";
import DetailDrawer_AddUpdate from "./components/DrawerAddUpdate";
import DetailDrawer_userInfo from "@/components/ViewComponents/PatientInformation/components/basicDrawer";
import { useDrawer } from "@/components/Drawer";
import { exportToExcel, TimeDate } from "@/utils/util";
import moment from "moment";
import { useState } from "react";
import { IconEnum } from "@/enums/appEnum";
import { Button } from "@/components/Button";

export default function emergency_medical_records() {
	const { t } = useTranslation();
	// let btnList = ["continue", "save", "edit", "skip", "print", "end"];
	let [getTitle, setGetTitle] = useState("");
	const [drawer_userInfo, { openDrawer: openDrawer_userInfo }] = useDrawer();
	const [drawer_history, { openDrawer: openDrawer_history }] = useDrawer();
	const [drawer_add_update, { openDrawer: opendDrawer_add_update }] = useDrawer();
	// 配置table
	const columns: BasicColumn[] = [
		{
			title: t("felix_ui.EmergencyID"),
			dataIndex: "emgId"
		},
		{
			title: t("felix_ui.time_of_arrival"),
			dataIndex: "emgDtime",
			render: text => <span>{TimeDate(text)}</span>
		},
		{
			title: t("felix_ui.referralType"),
			dataIndex: "referralType",
			render: (text: string) => <span>{util(text, "referralType")}</span>
		},
		{
			title: t("felix_ui.facId"),
			dataIndex: "facId"
		},
		{
			title: t("felix_ui.emgReason"),
			dataIndex: "emgReason"
		},
		{
			title: t("felix_ui.deptId"),
			dataIndex: "deptId",
			render: (text: string) => <span>{util(text, "deptId")}</span>
		},
		{
			title: t("felix_ui.status"),
			dataIndex: "status",
			render: (text: string, record: any) => <Switch checked={text === "1"} onChange={checked => switchChange(checked, record)} />
		},
		{
			title: t("felix_ui.ReferralID"),
			dataIndex: "transId"
		},
		{
			title: t("felix_ui.InsuranceID"),
			dataIndex: "insId"
		},
		{
			title: t("felix_ui.CheckID"),
			dataIndex: "examId"
		},
		{
			title: t("felix_ui.FamilyID"),
			dataIndex: "famId"
		},
		{
			title: t("felix_ui.operate"),
			key: "action",
			fixed: "right",
			render: (_, record: any) => (
				<TableAction
					actions={[
						{
							icon: IconEnum.EDIT,
							label: t("felix_ui.edit"),
							onClick: handleEdit.bind(null, record)
						}
					]}
					dropDownActions={[
						{
							icon: IconEnum.EDIT,
							label: t("felix_ui.pat_basic_info"),
							onClick: async () => {
								openDrawer_userInfo(true, {
									current: record.patientId ? await get_patients({ id: record.patientId }) : record
								});
							}
						},
						{
							icon: IconEnum.ADD,
							label: t("felix_ui.discharge_history"),
							onClick: () => {
								openDrawer_history(true, {
									patientId: record.patientId,
									patCardno: record.patCardno
								});
								// openDrawer_history(true, {
								// 	isUpdated: true, {
								// 		patientId: record.patientId,
								// 		patCardno: record.patCardno
								// 	}
								// })
							}
						},
						{
							icon: IconEnum.DELETE,
							label: t("felix_ui.delete"),
							popConfirm: {
								title: t("common.deleteMessage"),
								placement: "left",
								confirm: handleDel.bind(null, record)
							}
						}
					]}
				></TableAction>
			)
		}
	];
	let util = (val: string, dataIndex: string) => {
		if (dataIndex == "referralType") {
			switch (val) {
				case "1":
					return t("felix_ui.health_institutions");
					break;
				case "2":
					return t("felix_ui.not_mentioned");
					break;
				case "3":
					return t("felix_ui.orser");
					break;
				default:
					break;
			}
		} else if (dataIndex == "deptId") {
			switch (val) {
				case "1":
					return t("felix_ui.emergency_room");
					break;
				case "2":
					return t("felix_ui.examination_room");
					break;
				case "3":
					return t("felix_ui.treatment_room");
					break;
				default:
					break;
			}
		}
	};
	const searchFormSchema: FormSchema[] = [
		{
			label: t("felix_ui.EmergencyID"),
			field: "emgId",
			component: "Input",
			colProps: { span: 6 }
		},
		{
			label: t("felix_ui.patient_ID"),
			field: "patientId",
			component: "Input",
			colProps: { span: 6 }
		},
		{
			label: t("felix_ui.ReferralID"),
			field: "transId",
			component: "Input",
			colProps: { span: 6 }
		},
		{
			label: t("felix_ui.deptId"),
			field: "deptId",
			component: "Select",
			colProps: { span: 6 },
			componentProps: {
				options: [
					{ label: t("felix_ui.emergency_room"), value: "1" },
					{ label: t("felix_ui.examination_room"), value: "2" },
					{ label: t("felix_ui.treatment_room"), value: "3" }
				]
			}
		},
		{
			label: t("felix_ui.facId"),
			field: "facId",
			component: "Input",
			colProps: { span: 6 }
		},
		{
			label: t("felix_ui.status"),
			field: "status",
			component: "Select",
			componentProps: {
				options: [
					{ label: t("felix_ui.open"), value: "0" },
					{ label: t("felix_ui.close"), value: "1" }
				]
			},
			colProps: { span: 6 }
		}
	];
	// 配置table
	const [registerTable, { reload, updateTableDataRecord }] = useTable({
		api: get_emergencies_page,
		columns,
		// title: "Ⅱ." + t("felix_ui.inp_info"),
		title: t("felix_ui.EmergencyInfoList"),
		formConfig: {
			labelWidth: 120,
			schemas: searchFormSchema,
			showActionButtonGroup: true,
			showAdvancedButton: false
		},
		bordered: true,
		useSearchForm: true
	});
	// table状态修改
	const switchChange = (checked: boolean, record: any) => {
		console.log(checked, record);
		const newStatus = checked ? "1" : "0";
		let obj = { ...record, status: newStatus };
		update_emergencies(obj)
			.then(() => {
				updateTableDataRecord(record.key, { ...record, status: checked ? "1" : "0" });
			})
			.catch(() => {});
	};
	// 修改
	const handleEdit = async (record: Recordable) => {
		setGetTitle(t("felix_ui.edit_emergency_info"));
		opendDrawer_add_update(true, {
			record: { ...record, emgDtime: moment(TimeDate(record.emgDtime), "YYYY-MM-DD") }
		});
	};
	// 删除
	const handleDel = async (record: Recordable) => {
		console.log(record);
		await del_emergencies(record.emgId).then(() => {
			reload();
		});
	};
	// 新增
	const handleAdd = () => {
		setGetTitle(t("felix_ui.add_emergency_info"));
		opendDrawer_add_update(true, {});
	};
	// 导出
	const exportAction = async () => {
		await get_emergencies_page({}).then(res => {
			exportToExcel(res.list, t("felix_ui.pat_basic_info"));
		});
	};

	// const handleSubmitRegistration = async () => {
	// 	// try {
	// 	// 	const values1 = await validateFields();
	// 	// 	console.log("提交Form", values1);
	// 	// 	await create_emergencies(values1);
	// 	// } catch (err) {
	// 	// 	console.log(err);
	// 	// }
	// };
	// const handlerToolbarAction = (key: string) => {
	// 	switch (key) {
	// 		case "save":
	// 			handleSubmitRegistration();
	// 			break;
	// 	}
	// 	console.log("key", key);
	// };
	return (
		<div className="page w-100%">
			<div className="w-100%">
				<BasicTable register={registerTable}>
					<div slot="toolbar">
						<Button type="primary" preIcon={IconEnum.ADD} onClick={handleAdd}>
							{t("system.user.insertBtn")}
						</Button>
						<Button preIcon={IconEnum.EXPORT} onClick={exportAction}>
							{t("system.user.exportBtn")}
						</Button>
					</div>
				</BasicTable>
			</div>
			<DetailDrawer_AddUpdate getTitle={getTitle} reload={reload} register={drawer_add_update}></DetailDrawer_AddUpdate>
			<DetailDrawer_history register={drawer_history}></DetailDrawer_history>
			<DetailDrawer_userInfo register={drawer_userInfo}></DetailDrawer_userInfo>
			{/* <Toolbar action={handlerToolbarAction} btnList={btnList} /> */}
		</div>
	);
}
